package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

public class ThreadTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 1、继承Thread
         *         Thread01 thread = new Thread01();
         *         thread.start();启动线程
         *
         * 2、实现Runnable接口
         * Runable01 runable01 = new Runable01();
         *         new Thread(runable01).start();
         * 3、实现Callable接口+FutureTask（可以拿到返回结果，可以处理异常）
         * FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
         *         //阻塞等待整个线程执行完成，获得返回结果
         *         Integer integer = futureTask.get();
         *
         *
         * 4、线程池
         *  给线程池直接提交任务
         *  ExecutorService service  = Executors.newFixedThreadPool(10);
         *         service.execute(new Runable01());
         */
        //我们以后在业务代码里面，以上三种启动线程的方式都不用。【将所有的多线程异步任务都交给线程池执行】
        new Thread(()-> System.out.println("hello")).start();


        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
                200,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        System.out.println("main...end...");
    }

    public static class Thred01 extends Thread{
        @Override
        public void run(){
            System.out.println("当前线程： "+Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果： "+i);
        }
    }

    public static class Runable01 implements Runnable{
        @Override
        public void run() {
            System.out.println("当前教程：" +Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果：" +i);
        }
    }

    public static class Callable01 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程：" +Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果:" + i);
            return i;
        }
    }

}
